package com.myway.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Title : PaymentNoGenerator
 * Description : This is to generate the payment no
 * Payment No - yyyyMMddHHmmss000000 (6 digits of seq no)
 */
public class PaymentNoGenerator
{
	private static Log log = LogFactory.getLog(PaymentNoGenerator.class);

	public static String generate(long sequenceNo)
	{
		
		String seqNo = String.valueOf(sequenceNo);
		if(seqNo.length()>6){
			seqNo=seqNo.substring(seqNo.length()-6);
		}
		seqNo = padding(seqNo, 6) + seqNo;

		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		String dt = format.format(new Date());

		seqNo = dt + seqNo;

		log.debug("seqNo generatored= [" + seqNo + "]");

		return seqNo;
	}
	
	/**
	 * <pre>生成股份优惠机票支付流水号: yyyyMMddHHmmss + 5 + 7位序列号</pre>
	 * @param sequenceNo
	 * @return
	 */
	public static String generate4YHJP(long sequenceNo) {
		String seqNo = String.valueOf("0000000" + sequenceNo);
		seqNo= "5" + seqNo.substring(seqNo.length()-7);
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		String dt = format.format(new Date());

		seqNo = dt + seqNo;

		log.debug("seqNo generatored= [" + seqNo + "]");

		return seqNo;
	}

	private static String padding(String s, int i)
	{
		StringBuffer sb = new StringBuffer();
		if (s.length() < i)
		{
			for (int j = 0,j_total = i - s.length(); j < j_total; j++)
				sb.append("0");
		}
		return sb.toString();
	}
	
	public static void main(String[] args){
		generate(11112);
	}
}
